// JavaScript Document
/**
jBox.tip("正在XX，你懂的...", 'loading');
jBox.tip("正在XX，你懂的...", 'info');
jBox.tip("正在XX，你懂的...", 'success');
jBox.tip("正在XX，你懂的...", 'warning');
jBox.tip("正在XX，你懂的...", 'error');
*/


//列表页  单行删除 jbxo  ajax
 $(function(){
    var $btn_del =  $('.btn_del');
    var lock = false;
    $btn_del.on('click',function(){
        var btn = $(this);

        var _del= function(){
            var url = btn.attr('href'); 
            var $tr = btn.closest('tr');

            if(lock ){ return false ;}else{ lock = true  ;}
            $.ajax({
                url:url,
                type:'post',
                data:{},
                dataType:'json',
                success:function(d){
                    if(d.ret == 0 ){
                        jBox.tip(d.msg, 'success',{closed: function () {
                            $tr.fadeOut(800);
                        }});

                        lock=false;

                    }else{
                        jBox.tip(d.msg, 'error');
                        lock=false;
                    }
                }

            });
        };

        jBox.confirm("确定吗？", "提示", function(v, h, f){
            if(v == 'ok'){
                 _del();
            }else{
              
            }

        });
    
        return false;//取消a链接的跳转
    });
 });

//列表页  单行撤销删除 jbxo  ajax
 $(function(){
    var $btn =  $('.btn_back');
    var lock = false;
    $btn.on('click',function(){
        var btn = $(this);

        var _back= function(){
            var url = btn.attr('href'); 
            var $tr = btn.closest('tr');

            if(lock ){ return false ;}else{ lock = true  ;}
            $.ajax({
                url:url,
                type:'post',
                data:{},
                dataType:'json',
                success:function(d){
                    if(d.ret == 0 ){
                        jBox.tip(d.msg, 'success',{closed: function () {
                            $tr.fadeOut(800);
                        }});

                        lock=false;

                    }else{
                        jBox.tip(d.msg, 'error');
                        lock=false;
                    }
                }

            });
        };

        jBox.confirm("确定撤销删除吗？", "提示", function(v, h, f){
            if(v == 'ok'){
                 _back();
            }else{
              
            }

        });
    
        return false;//取消a链接的跳转
    });
 });

 // 多选框 全选
 // <input type="checkbox" cbGroupControl='A'/>
 // <input type="checkbox" cbGroup='A' />
  $(function(){
    $('[cbGroupControl]').on('click', function(){
        var that = $(this);

        var cbGroup = that.attr('cbGroupControl'); // A
        var checkbox = $('[cbGroup='+cbGroup+']'); // [cbGroup='A']


        var a = $(this).prop('checked');
        if( a ){
            checkbox.prop('checked',true);

        }else{
            checkbox.prop('checked',false);
        }
        
        //$('input[type=checkbox]').attr('checked',false );
    })

 });


 // 显示隐藏批量 操作 容器
  $(function(){
    var $batchControl = $('.batchControl');
    $('[cbGroup=A] , [cbGroupControl=A]' ).on('click',function(){

        var num = 0
        $('[cbGroup=A]').each(function(){

            var flag = $(this).prop('checked');
            if( flag ){
                num++;
            }
        });

        if( num>0 ){
            $batchControl.show();

        }else{
            $batchControl.hide();
        }
    });

 });


//批量操作 
    $(function(){

        var lock;
        var _loading= function(){};
        var _finish = function(){};

        var form  = $('.listForm');

        $('[btnbatchupdate]').on('click',function(){

            
            var a  = $(this).attr('btnbatchupdate');

            form.attr('action','?dopost='+a);

            ajaxbatchupdate(form);

            return false;
        });

        function  ajaxbatchupdate(form){

            var url = form.attr('action');
            var type = form.attr('method');




            if( lock ){ return false ;}else{ lock = true ;}
            jBox.tip("数据提交中...", 'loading');
            _loading();


            $.ajax({
                url:url,
                type:type,
                data: form.serialize(), // url查询字符串  xxx=111&xxx=222
                dataType:"json",
                success:function(d){

                    if(d.ret == 0 ){
                        jBox.tip(d.msg, 'success');
                        setTimeout(function(){
                           //window.location.href= d.url ; 
                           window.location.reload();
                        },3000);
                        _finish();
                    }else if(d.ret == 666){
						jBox.tip(d.msg, 'info');
                        setTimeout(function(){
                           //window.location.href= d.url ; 
                           window.location.reload();
                        },3000);
						_finish();
					}else{

                        jBox.tip(d.msg, 'error');
                        lock = false;
                    }


                }



            });
        }



    });




